package cn.sumpu.app.guoguojie.biz.impl;

import java.io.IOException;
import java.util.*;

import javax.servlet.ServletContext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ServletContextAware;

import cn.sumpu.app.guoguojie.biz.*;
import cn.sumpu.app.guoguojie.constant.Channel;
import cn.sumpu.app.guoguojie.domain.*;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.*;
import freemarker.template.TemplateException;

public class BuildHtmlBizImpl implements BuildHtmlBiz,ServletContextAware,
		InitializingBean {
    @Autowired
	private CmsContentBiz cmsContentBiz;
	
	@Autowired
	private TopShopBiz topShopBiz;
	@Autowired
	private DiscussBiz discussBiz;
	@Autowired
    private DoingBiz doingBiz;
	private ServletContext servletContext;  
	
	
	
	@Override
    public void afterPropertiesSet() throws Exception {
	    String path = servletContext.getRealPath("/");
        builIndexHtml(path);
       System.out.println("生成首页成功");
    }

	
	 @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext=servletContext;  
    }






    //	生成首页
	public void builIndexHtml(String path) throws AppBusinessException, IOException, TemplateException{
	    String templatePath = path + "/WEB-INF/views/v3/index";
        String templateName = "index.ftl";
        String fileName = path + "/index.html";

        Map<String, Object> data = new HashMap<String, Object>();
        List<CmsContent> banner = cmsContentBiz.getContentList("f2cc760d-1b9c-4806-8504-df31e093527c", 3);
        data.put("banner", banner);
        //          最热关键词
        List<CmsContent> hotKeyWords = cmsContentBiz.getContentList("e14782c6-1bc1-4923-9371-a6f2c9887485", 1);
        if (hotKeyWords != null && hotKeyWords.size() > 0) {
            data.put("hotKeyWords", hotKeyWords.get(0));
        }else{
            data.put("hotKeyWords", null);
        }
        //          首页轮播图
        List<CmsContent> lunboList = cmsContentBiz.getContentList("ae7b7f6f-0a82-4cc3-bb9f-e0b475978347", 5);
        data.put("lunboList", lunboList);
        //          首页轮播图右侧
        List<CmsContent> lunboRight = cmsContentBiz.getContentList("da03454e-56a2-4594-8ef2-806aa87e5144", 5);
        data.put("lunboRight", lunboRight);
        //          通栏广告
        List<CmsContent> commonAdv = cmsContentBiz.getContentList("9e1d74eb-b666-4a98-8ff6-2298984c35c3", 4);
        data.put("commonAdv", commonAdv);
        
        //         人气推荐
        List<CmsContent> popRec = cmsContentBiz.getContentList("23161d66-b1f7-4bbd-ad2d-da1624273cb8", 11);
        data.put("popRec", popRec);  
//        限时返利
        List<DoingProductTime> fanliList = doingBiz.getFanliProduct("fc6eeae6-cb5c-45c1-ab7f-7bc2712bfd28", 5);
        data.put("fanliList", fanliList);
//      限时返利右边栏
        List<CmsContent> fanliRight = cmsContentBiz.getContentList("5bb7b3b5-96fd-446c-9f67-86adc7c3ad9d", 3);
        data.put("fanliRight", fanliRight); 
//      好店
        List<TaobaokeShop> shopList = topShopBiz.getIndexShop();
        data.put("shopList", shopList);
//        友情链接
        List<CmsContent> fList = cmsContentBiz.getContentList("5d575e2b-3b63-4f6c-9b7e-aade27353510", 50);
        data.put("fList", fList);
        
//      妈妈帮淘
        List<CmsContent> helptaoList = discussBiz.getAmoysList(8);
        data.put("helptaoList", helptaoList); 
//      0-1岁要淘的东西和标签
        List<CmsContent> tao01List = discussBiz.getContentListById(Channel.TAO_HELP_01, 8);
        data.put("tao01List", tao01List);
        List<DiscussTag>   tag01List = discussBiz.getTagByCid(Channel.TAO_HELP_01);
        data.put("tag01List", tag01List);
        
//      1-3岁要淘的东西
        List<CmsContent> tao13List = discussBiz.getContentListById(Channel.TAO_HELP_13, 8);
        data.put("tao13List", tao13List);
        List<DiscussTag>   tag13List = discussBiz.getTagByCid(Channel.TAO_HELP_13);
        data.put("tag13List", tag13List);
//      3-6岁要淘的东西
        List<CmsContent> tao36List = discussBiz.getContentListById(Channel.TAO_HELP_36, 8);
        data.put("tao36List", tao36List);
        List<DiscussTag>   tag36List = discussBiz.getTagByCid(Channel.TAO_HELP_36);
        data.put("tag36List", tag36List);
//       怀孕期要淘的东西
        List<CmsContent> huaiyunList = discussBiz.getContentListById(Channel.TAO_HELP_HUAIYUN, 8);
        data.put("huaiyunList", huaiyunList);
        List<DiscussTag>   taghyList = discussBiz.getTagByCid(Channel.TAO_HELP_HUAIYUN);
        data.put("taghyList", taghyList);
//        底部
        List<CmsContent> footer = cmsContentBiz.getContentList("d74dc9bb-b312-40f7-956f-1f946874ba26", 2);
        data.put("footer", footer);
        
        FreeMarkerUtil.analysisTemplate(templatePath, templateName, fileName, data);
	}


    @Override
    public void builEdmHtml(String path) throws AppBusinessException, IOException, TemplateException {
        String templatePath = path + "/WEB-INF/views/v3/edm";
        String templateName = "edm.ftl";
        String fileName = path + "/edm.html";

        
        Map<String, Object> data = new HashMap<String, Object>();
//        返利产品列表
        List<DoingProductTime> dptList = doingBiz.getEdmProduct();
        data.put("dptList", dptList); 
//        EMD头
        List<CmsContent> edmTitle = cmsContentBiz.getContentList("e5077042-9bf2-4570-a992-041bd1bfb06b", 1);
        if(edmTitle!=null && edmTitle.size() > 0){
            data.put("edmTitle", edmTitle.get(0));
        }
        FreeMarkerUtil.analysisTemplate(templatePath, templateName, fileName, data);
    }
	
	

	
}
